import pygame
import random
from pygame.sprite import Group
from spider import Spider


class SpiderFleet(Group):
    def __init__(self, screen):
        super().__init__()
        self.screen = screen
        (scr_w, scr_h) = self.screen.get_size()
        self.scr_h = scr_h
        self.fleet_x_range = scr_w
        self.fleet_y_range = scr_h / 8
        pygame.time.set_timer(pygame.USEREVENT + 1, 1000)

    def add_new_spider(self):
        spider_x = random.randrange(0, self.fleet_x_range, 1)
        spider_y = random.randrange(0, self.fleet_y_range, 1)
        new_spider = Spider(self.screen, spider_x, spider_y)
        self.add(new_spider)

    def moving_update(self):
        for spider in self.__iter__():
            spider.cy += spider.speed_factor
            spider.rect.centery = spider.cy
            if spider.rect.top > self.scr_h:
                self.remove(spider)
                #print("delete spider ok")
            spider.blit_spider()
